package com.haoan.index.service.impl;


import java.util.List;
import javax.annotation.Resource;

import com.haoan.index.domain.AnnualPlace;
import com.haoan.index.mapper.AnnualPlaceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoan.index.common.enums.ValiFlag;

import com.haoan.index.mapper.AnnualPassMapper;
import com.haoan.index.domain.AnnualPass;
import com.haoan.index.service.AnnualPassService;

/**
 * 年卡阵地Service业务层处理
 *
 * @author autoGen
 * @date 2025-01-03
 */
@Service
public class AnnualPassServiceImpl extends ServiceImpl<AnnualPassMapper,AnnualPass> implements AnnualPassService
{

    @Resource
    private AnnualPlaceMapper annualPlaceMapper;
    /**
     * 查询年卡阵地
     *
     * @param id 年卡阵地主键
     * @return 年卡阵地
     */
    @Override
    public AnnualPass selectAnnualPassById(Long id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询年卡阵地列表
     *
     * @param annualPass 年卡阵地
     * @return 年卡阵地
     */
    @Override
    public List<AnnualPass> selectAnnualPassList(AnnualPass annualPass)
    {
        LambdaQueryWrapper<AnnualPass> queryWrapper = new LambdaQueryWrapper<>();
        //todo 添加查询条件
        queryWrapper.eq(annualPass.getId() != null, AnnualPass::getId, annualPass.getId());
        queryWrapper.like(annualPass.getNkName() != null, AnnualPass::getNkName, annualPass.getNkName());
        queryWrapper.eq(annualPass.getWxPicture() != null, AnnualPass::getWxPicture, annualPass.getWxPicture());
        queryWrapper.eq(annualPass.getAliPicture() != null, AnnualPass::getAliPicture, annualPass.getAliPicture());
        queryWrapper.eq(annualPass.getNkMeitu() != null, AnnualPass::getNkMeitu, annualPass.getNkMeitu());
        queryWrapper.eq(annualPass.getCreateTime() != null, AnnualPass::getCreateTime, annualPass.getCreateTime());
        queryWrapper.eq(annualPass.getCreateBy() != null, AnnualPass::getCreateBy, annualPass.getCreateBy());
        queryWrapper.eq(annualPass.getUpdateTime() != null, AnnualPass::getUpdateTime, annualPass.getUpdateTime());
        queryWrapper.eq(annualPass.getUpdateBy() != null, AnnualPass::getUpdateBy, annualPass.getUpdateBy());
        queryWrapper.eq(AnnualPass::getValiFlag,ValiFlag.VALID );
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 新增年卡阵地
     *
     * @param annualPass 年卡阵地
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAnnualPass(AnnualPass annualPass)
    {
        baseMapper.insert(annualPass);
        if (null != annualPass.getAnnualPlaceList()){
            for (AnnualPlace annualPlace : annualPass.getAnnualPlaceList()) {
                annualPlace.setAnnualId(annualPass.getId());
                annualPlaceMapper.insert(annualPlace);
            }
        }
        return 1;
    }

    /**
     * 修改年卡阵地
     *
     * @param annualPass 年卡阵地
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAnnualPass(AnnualPass annualPass)
    {
        LambdaQueryWrapper<AnnualPlace> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AnnualPlace::getAnnualId,annualPass.getId());
        annualPlaceMapper.delete(queryWrapper);
        baseMapper.updateById(annualPass);
        if (null != annualPass.getAnnualPlaceList()){
            for (AnnualPlace annualPlace : annualPass.getAnnualPlaceList()) {
                annualPlace.setAnnualId(annualPass.getId());
                annualPlaceMapper.insert(annualPlace);
            }
        }
        return 1;
    }

    /**
     * 批量删除年卡阵地
     *
     * @param ids 需要删除的年卡阵地主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAnnualPassByIds(Long [] ids)
    {
        AnnualPass annualPass = new AnnualPass();
        annualPass.setValiFlag(ValiFlag.INVALID);
        LambdaQueryWrapper<AnnualPass> updQw = new LambdaQueryWrapper<>();
        updQw.in(AnnualPass::getId, ids);
        return baseMapper.update(annualPass, updQw);
    }

    /**
     * 删除年卡阵地信息
     *
     * @param id 年卡阵地主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteById(Long id)
    {
        AnnualPass annualPass = getById(id);
        annualPass.setValiFlag(ValiFlag.INVALID);
        return baseMapper.updateById(annualPass);
    }
}
